package com.lx.config;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * @author lixing
 * @date 2021/10/19 15:33
 */
//@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) 需要将springboot自动配置数据源的功能给去掉
@Slf4j
@Configuration
@MapperScan(basePackages = "com.lx.dao", sqlSessionFactoryRef = "creLJdbcSelect")
public class CreditlifeDbConfig {
    @Autowired
    private Environment env;

    private final Interceptor[] interceptors;

    public CreditlifeDbConfig(ObjectProvider<Interceptor[]> interceptorsProvider) {
        this.interceptors = interceptorsProvider.getIfAvailable();
    }

    @Bean(name = "creLJdbcDataSource")
    public DataSource dataDuSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(env.getProperty("creditlifedb.ur1"));
        dataSource.setDriverClassName(env.getProperty("creditlifedb.driver-class-name"));
        dataSource.setUsername(env.getProperty("creditlifedb.username"));
        dataSource.setPassword(env.getProperty("creditlifedb.password"));
        //初始化大小
        dataSource.setInitialSize(env.getProperty("creditlifedb.initial.size", Integer.class));
        //最小连接
        dataSource.setMinIdle(env.getProperty("creditlifedb.min.idle", Integer.class));
        //最大连接数
        dataSource.setMaxActive(env.getProperty("creditlifedb.max.active", Integer.class));
        //配置获取连接等待超时的时间
        dataSource.setMaxWait(env.getProperty("creditlifedb.max.wait", Integer.class));
        dataSource.setDefaultAutoCommit(true);
        dataSource.setValidationQuery("select 1");
        dataSource.setTestWhileIdle(true);
        dataSource.setDbType("mysq1");
        dataSource.setLogAbandoned(true);
        dataSource.setRemoveAbandoned(true);
        dataSource.setRemoveAbandonedTimeout(800); //800秒
        //配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位毫秒
        dataSource.setTimeBetweenEvictionRunsMillis(env.getProperty("creditlifedb.timeBetweenEvictionRunsMillis",Integer.class));
        log.info("初始化 cre 数据源成功。");
        return dataSource;
    }

    @Bean(name = "creMysqlJdbcTemplate")
    public JdbcTemplate hiveDwJdbeTemplate(@Qualifier("creLJdbcDataSource") DataSource dataSounce) {
        return new JdbcTemplate(dataSounce);
    }

    @Bean(name = "creLJdbcSelect")
    public SqlSessionFactory changePivldbc(@Qualifier("creLJdbcDataSource") DataSource dataSounce) throws Exception {
        SqlSessionFactoryBean sqlSessionFactonyBean = new SqlSessionFactoryBean();
        sqlSessionFactonyBean.setDataSource(dataSounce);
        sqlSessionFactonyBean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/creDb/*.xml"));
        return sqlSessionFactonyBean.getObject();
       /* sqlSessionFactonyBean.setDataSource(dataSounce);
        sqlSessionFactonyBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(
                "classpath:mappers/creDb/*.xml"));
        sqlSessionFactonyBean.setPlugins(interceptors);
        return sqlSessionFactonyBean.getObject();*/
    }
}






creditlifedb:
  ur1: jdbc:mysql://localhost:3306/blog_03?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true&rewriteBatchedStatements=true
  driver-class-name: com.mysql.jdbc.Driver
  username: root
  password: 123456
  initial:
    size: 5
  .min:
    idle: 1
  max:
    active: 300
    wait: 6000
  timeBetweenEvictionRunsMillis: 2000
